package com.cfxy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cfxy.dao.UserDao;
import com.cfxy.model.User;

@WebServlet("/usermanager")
public class UserManager extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String func = request.getParameter("funcation");
		if("login".equals(func))login(request,response);
		else if("add".equals(func))add(request,response);
		else if("delete".equals(func))delete(request,response);
		else if("Edit".equals(func))Edit(request,response);
		else if("update".equals(func))update(request,response);
		else show(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		if(request.getParameter("submitType").equals("登录")){
			String name = request.getParameter("username");
			String psw = request.getParameter("password");			
			User user = new UserDao().find(name,psw);
			if(user == null){
				response.sendRedirect("login.jsp");
			}else{
				request.getSession().setAttribute("uuser",user);
				request.getRequestDispatcher("/messagemanager?funcation=show").forward(request, response);
				//show(request,response);
			}
		}else{
			response.sendRedirect("register.jsp");
		}
	}
	public void add(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("username");
		String psw = request.getParameter("password");
		new UserDao().insert(name,psw);
		try {
			show(request,response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		new UserDao().delet(Integer.parseInt(id));
		show(request,response);
	}
	public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		UserDao udao = new UserDao();
		List<User> users = udao.getAll();
		request.setAttribute("ulist",users);
		request.getRequestDispatcher("result.jsp").forward(request, response);
	}
	public void Edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		User user =new UserDao().find(Integer.parseInt(id));
		request.setAttribute("name",user.getUsername());
		request.setAttribute("psw",user.getPassword());
		request.setAttribute("id",user.getId());
		request.getRequestDispatcher("edituser.jsp").forward(request, response);
		
	}
	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		String name = request.getParameter("username");
		String psw =request.getParameter("password");
		User user = new User();
		user.setId(Integer.parseInt(id));
		user.setUsername(name);
		user.setPassword(psw);
		new UserDao().upDate(user);
		show(request,response);
	}

}
